/**
 * activiti js帮助方法
 */

var setReadonly = function (result) {
    var properties = result.properties;
    for(var i=0;i<properties.length;i++){
        var resultObj = properties[i];
        var id = resultObj.id;
        $('#'+id).attr("disabled","disabled");
    }

};

/**根据activiti后台动态表单数据  动态生成表单html*/
var coverHtml = function (result) {
    var html = "";
    var properties = result.properties;
    for(var i=0;i<properties.length;i++){
        var resultObj = properties[i];
        var typeName = resultObj.typeMap.name;
        if(typeName==="long"){
            html += createLong(resultObj);
        }else if(typeName==="string"){
            html += createString(resultObj);
        }else if(typeName==="enum"){
            html += createSelect(resultObj);
        }else if(typeName==="date"){
            html += createDate(resultObj);
        }
    }

    return html;

};

var createLong = function (messageInfo) {
    var formName = messageInfo.name;
    var id = messageInfo.id;
    var readable = messageInfo.readable;
    var required = messageInfo.required;
    var writable = messageInfo.writable;
    var value = messageInfo.value;

    var html = "";
    //是否可读
    if(!readable){
        html +="<div hidden=\"hidden\" > ";
    }

    html += "<div class=\"form-group\"> ";
    html += "<label class=\"col-sm-3 control-label\">"+formName;
    //必填项
    if(required){
        html += "<span style=\"color: red;\">*</span>";
    }
    html +="</label> "
    html +="<div class=\"col-sm-7\"> "
    html +="<input type=\"text\" class=\"form-control\" id=\""+id+"\" name=\""+id+"\" " +
        "onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'')\" onafterpaste=\"this.value=this.value.replace(/[^0-9]/g,'')\" ";
    //只读
    if(!writable){
        html += "readonly=\"readonly\" ";
    }
    //必填项
    if(required){
        html +="onBlur=\"onBlurValue(this)\" ";
    }
    //预填值
    if(value!=null&&value!=''){
        html +="value=\""+value+"\" ";
    }
    html +="/> ";
    //必填项
    if(required){
        html +="<span class=\"help-block\" style=\"color: red; display: none;\">"+formName+"</span>  ";
    }

    html +="</div> " +
        "</div> ";
    //是否可读
    if(!readable){
        html +="</div> ";
    }

    return html;
};

var createString = function (messageInfo) {
    var formName = messageInfo.name;
    var id = messageInfo.id;
    var readable = messageInfo.readable;
    var required = messageInfo.required;
    var writable = messageInfo.writable;
    var value = messageInfo.value;

    var html = "";
    //是否可读
    if(!readable){
        html +="<div hidden=\"hidden\" > ";
    }

    html += "<div class=\"form-group\"> " +
        "<label class=\"col-sm-3 control-label\">"+formName;
    //必填项
    if(required){
        html += "<span style=\"color: red;\">*</span>";
    }
    html +="</label> " +
        "<div class=\"col-sm-7\"> " ;
    html += "<input type=\"text\" class=\"form-control\" id=\""+id+"\" name=\""+id+"\" ";
    //只读
    if(!writable){
        html += "readonly=\"readonly\" ";
    }
    //必填项
    if(required){
        html +="onBlur=\"onBlurValue(this)\" ";
    }
    //预填值
    if(value!=null&&value!=''){
        html +="value=\""+value+"\" ";
    }
    html +="/> ";
    //必填项
    if(required){
        html +="<span class=\"help-block\" style=\"color: red; display: none;\">"+formName+"</span>  ";
    }

    html += "</div> " +
        "</div> ";
    //是否可读
    if(!readable){
        html +="</div> ";
    }

    return html;
};

var createSelect = function (messageInfo) {
    var formName = messageInfo.name;
    var id = messageInfo.id;
    var readable = messageInfo.readable;
    var required = messageInfo.required;
    var writable = messageInfo.writable;
    var value = messageInfo.value;
    var resultMessage = messageInfo.typeMap.values;

    var html = "";

    html +=" <div class=\"form-group\"> " +
        "        <label class=\"col-sm-3 control-label\">"+formName;
    if(required){
        html += "<span style=\"color: red;\">*</span> ";
    }
    html += "</label> ";
    html +="<div class=\"col-sm-7\"> " +
        "            <select class=\"form-control\" id=\""+id+"\" name=\""+id+"\" style=\"width: 30%\"> "
    for (var key in resultMessage) {
        var value = resultMessage[key];
        html +="<option value=\""+key+"\" >"+value+"</option>";
    }

    html +="       </select> " +
        "        </div> " +
        "    </div> ";

    return html;
};

var createDate = function (messageInfo) {
    var formName = messageInfo.name;
    var id = messageInfo.id;
    var readable = messageInfo.readable;
    var required = messageInfo.required;
    var writable = messageInfo.writable;
    var value = messageInfo.value;

    var html = "";

    html +=" <div class=\"form-group\"> " +
        "        <label class=\"col-sm-3 control-label\">"+formName;
    if(required){
        html += "<span style=\"color: red;\">*</span>" ;
    }
    html +="</label> "
    html +="<div class=\"col-sm-7\"> " +
        "            <input type=\"text\"  value=\"\" id=\""+id+"\" name=\""+id+"\"> " +
        "        </div> " +
        "    </div> "

    return html;
};

/**时间选择框添加控件*/
var createDateController = function(messageInfo){
    var properties = messageInfo.properties;
    for(var i=0;i<properties.length;i++){
        var resultObj = properties[i];
        var typeName = resultObj.typeMap.name;
        var datePattern = resultObj.typeMap.datePattern;
        var id = resultObj.id;
        if(typeName==="date"){
            $('#'+id).fdatepicker({
                format: 'yyyy-mm-dd hh:ii',//'yyyy-mm-dd hh:ii',datePattern
                pickTime: true
            });
        }
    }
};

var insertValue = function (messageInfo) {
    for(var key in messageInfo){
        var value = messageInfo[key];
        $("#"+key).val(value);
    }
};
